package com.ck.controller.teacher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ck.domain.Chapter;
import com.ck.domain.Tiankong;
import com.ck.service.QuestionService;

public class AddNewTiankongController extends SimpleFormController {
	
	private QuestionService questionService;

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	//公共变量，用于表单回显
	private List<Chapter> chapterList = null;
	
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		Map model = new HashMap();
		chapterList = questionService.gainChapterList();
		model.put("chapterList", chapterList);
		return model;
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Tiankong tiankong = (Tiankong) command;
		Map model = errors.getModel();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		
		//后台校验
		boolean flag = true;
		String description_before = tiankong.getDescription_before().trim();
		String description_after = tiankong.getDescription_after().trim();
		String correct_answer = tiankong.getCorrect_answer().trim();
		String synonym_one = tiankong.getSynonym_one().trim();
		String synonym_two = tiankong.getSynonym_two().trim();
		String synonym_three = tiankong.getSynonym_three().trim();
		Integer difficulty = tiankong.getDifficulty();
		Integer score = tiankong.getScore();
		Integer chapterID = tiankong.getChapter();
		String remark = tiankong.getRemark().trim();
		
		if( (description_before == null || "".equals(description_before)) && (description_after == null || "".equals(description_after)) ) {
			model.put("error_description_before", "空前描述和空后描述不能全为空");
			flag = false;
		}
		
		if(description_before.length() > 500) {
			model.put("error_description_before", "空前描述长度：0-500个字符");
			flag = false;
		}
		
		if(description_after.length() > 500) {
			model.put("error_description_after", "空后描述长度：0-500个字符");
			flag = false;
		}
		
		if(correct_answer == null || "".equals(correct_answer)) {
			model.put("error_correct_answer", "请输入正确答案");
			flag = false;
		}
		
		if(correct_answer.length() > 100) {
			model.put("error_correct_answer", "正确答案长度：1-100个字符");
			flag = false;
		}
		
		if( synonym_one.length()>100 || synonym_two.length()>100 || synonym_three.length()>100 ) {
			model.put("error_synonym", "每个近义词答案长度：0-100个字符");
			flag = false;
		}

		if( difficulty == 0){
			model.put("error_difficulty", "请选择难度");
			flag = false;
		}
		
		if(score == null){
			model.put("error_score", "请输入分值");
			flag = false;
		}else{
			if(score<1 || score>99){
				model.put("error_score", "分值须为1-99的整数");
				flag = false;
			}
		}
		
		if( chapterID == 0){
			model.put("error_chapterID", "请选择章节");
			flag = false;
		}
		
		if( remark == null || "".equals(remark) ){
			tiankong.setRemark(null);
        }else{
        	if(remark.length() > 100) {
    			model.put("error_remark", "备注长度：0-100个字符");
    			flag = false;
    		}
        }
		
		//校验不通过
        if(!flag){
        	model.put("description_before", description_before);
        	model.put("description_after", description_after);
        	model.put("correct_answer", correct_answer);
        	model.put("synonym_one", synonym_one);
        	model.put("synonym_two", synonym_two);
        	model.put("synonym_three", synonym_three);
        	model.put("difficulty", difficulty);
        	model.put("score", score);
        	model.put("chapterID", chapterID);
        	model.put("remark", remark);
        	model.put("chapterList", chapterList);
			return new ModelAndView(getFormView(), model);
		}
		
		String pic = questionService.uploadImage(multipartRequest, model);
		tiankong.setPic(pic);
		
		boolean addTiankongSuccess = questionService.addNewTiankong(tiankong, model);
		
		if(!("error".equals(pic)) && addTiankongSuccess) {		
			return new ModelAndView(getSuccessView(),model);
		} else {
			model.put("description_before", description_before);
        	model.put("description_after", description_after);
        	model.put("correct_answer", correct_answer);
        	model.put("synonym_one", synonym_one);
        	model.put("synonym_two", synonym_two);
        	model.put("synonym_three", synonym_three);
        	model.put("difficulty", difficulty);
        	model.put("score", score);
        	model.put("chapterID", chapterID);
        	model.put("remark", remark);
			model.put("chapterList", chapterList);
			return new ModelAndView(getFormView(), model);
		}
	}
}
